Passed
Pull Request — master (#124)
by Alejandro
04:16 queued 01:17
created

utils.test.js ➔ ???   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 95
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 61
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 95
rs 8.2763

1 Function

Rating   Name   Duplication   Size   Complexity  
A utils.test.js ➔ ... ➔ ??? 0 17 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import * as sinon from 'sinon';
2
import L from 'leaflet';
3
import marker2x from 'leaflet/dist/images/marker-icon-2x.png';
4
import marker from 'leaflet/dist/images/marker-icon.png';
5
import markerShadow from 'leaflet/dist/images/marker-shadow.png';
6
import {
7
  stateFlagTimeout as stateFlagTimeoutFactory,
0 ignored issues
show
Unused Code introduced by
The variable stateFlagTimeout seems to be never used. Consider removing it.
Loading history...
8
  determineOrderDir,
9
  fixLeafletIcons,
10
  rangeOf,
11
  roundTen,
12
} from '../../src/utils/utils';
13
14
describe('utils', () => {
15
  describe('stateFlagTimeout', () => {
16
    it('sets state and initializes timeout with provided delay', () => {
17
      const setTimeout = sinon.fake((callback) => callback());
18
      const setState = sinon.spy();
19
      const stateFlagTimeout = stateFlagTimeoutFactory(setTimeout);
20
      const delay = 5000;
21
      const expectedSetStateCalls = 2;
22
23
      stateFlagTimeout(setState, 'foo', false, delay);
24
25
      expect(setState.callCount).toEqual(expectedSetStateCalls);
26
      expect(setState.getCall(0).args).toEqual([{ foo: false }]);
27
      expect(setState.getCall(1).args).toEqual([{ foo: true }]);
28
      expect(setTimeout.callCount).toEqual(1);
29
      expect(setTimeout.getCall(0).args[1]).toEqual(delay);
30
    });
31
  });
32
33
  describe('determineOrderDir', () => {
34
    it('returns ASC when current order field and selected field are different', () => {
35
      expect(determineOrderDir('foo', 'bar')).toEqual('ASC');
36
      expect(determineOrderDir('bar', 'foo')).toEqual('ASC');
37
    });
38
39
    it('returns ASC when no current order dir is provided', () => {
40
      expect(determineOrderDir('foo', 'foo')).toEqual('ASC');
41
      expect(determineOrderDir('bar', 'bar')).toEqual('ASC');
42
    });
43
44
    it('returns DESC when current order field and selected field are equal and current order dir is ASC', () => {
45
      expect(determineOrderDir('foo', 'foo', 'ASC')).toEqual('DESC');
46
      expect(determineOrderDir('bar', 'bar', 'ASC')).toEqual('DESC');
47
    });
48
49
    it('returns undefined when current order field and selected field are equal and current order dir is DESC', () => {
50
      expect(determineOrderDir('foo', 'foo', 'DESC')).toBeUndefined();
51
      expect(determineOrderDir('bar', 'bar', 'DESC')).toBeUndefined();
52
    });
53
  });
54
55
  describe('fixLeafletIcons', () => {
56
    it('updates icons used by leaflet', () => {
57
      fixLeafletIcons();
58
59
      const { iconRetinaUrl, iconUrl, shadowUrl } = L.Icon.Default.prototype.options;
60
61
      expect(iconRetinaUrl).toEqual(marker2x);
62
      expect(iconUrl).toEqual(marker);
63
      expect(shadowUrl).toEqual(markerShadow);
64
    });
65
  });
66
67
  describe('rangeOf', () => {
68
    const func = (i) => `result_${i}`;
69
    const size = 5;
70
71
    it('builds a range of specified size invike provided function', () => {
72
      expect(rangeOf(size, func)).toEqual([
73
        'result_1',
74
        'result_2',
75
        'result_3',
76
        'result_4',
77
        'result_5',
78
      ]);
79
    });
80
81
    it('builds a range starting at provided pos', () => {
82
      const startAt = 3;
83
84
      expect(rangeOf(size, func, startAt)).toEqual([
85
        'result_3',
86
        'result_4',
87
        'result_5',
88
      ]);
89
    });
90
  });
91
92
  describe('roundTen', () => {
93
    it('rounds provided number to the next multiple of ten', () => {
94
      const expectationsPairs = [
95
        [ 10, 10 ],
96
        [ 12, 20 ],
97
        [ 158, 160 ],
98
        [ 5, 10 ],
99
        [ -42, -40 ],
100
      ];
101
102
      expect.assertions(expectationsPairs.length);
0 ignored issues
show
Bug introduced by
The variable expect seems to be never declared. If this is a global, consider adding a /** global: expect */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
103
      expectationsPairs.forEach(([ number, expected ]) => {
104
        expect(roundTen(number)).toEqual(expected);
105
      });
106
    });
107
  });
108
});
109